import {
	uploadImageBase64
} from "../api/module/common";
import {
	editUserInfo
} from "../api/module/mine";
export const mobileRegular = /^1[3456789]\d{9}$/
export const idCardRegular = /(^\d{15}$)|(^\d{17}([0-9]|X)$)/
export const passwordRegular = /^([a-z]|[A-Z]|[0-9]){8,}$/
export const pageUrl = {
	common: {
		pageDetailUrl: '/pages/article/detail',
		pageListUrl: '/pages/article/index',
		fileUrl: '/pages/my/file/index',
		collectUrl: '/pages/my/collect/index',
	},
	realnameUrl: {
		1: '/pages/my/person/Realname/index',
		2: '/pages/my/Join-enterprise'
	},
	sealUploadUrl: {
		2: '/pages/my/enterprise/seal/upload',
	},
	sealDetailUrl: {
		2: '/pages/my/enterprise/seal/detail',
	},
	sealListUrl: {
		2: '/pages/my/enterprise/seal/index',
	},
	realNameUrl: {
		1: '/pages/my/person/Realname/index',
	},
	userInfoUrl: {
		1: '/pages/my/person/userInfo/index',
		2: '/pages/my/enterprise/userInfo/index',
	},
	contractDetailUrl: {
		1: '/pages/contract/person/detail',
		2: '/pages/contract/enterprise/detail',
	},
	templateUrl: {
		1: '/pages/contract/person/templateList',
		2: '/pages/contract/enterprise/templateList',
	},
	initiateUrl: {
		1: '/pages/contract/person/initiate',
		2: '/pages/contract/enterprise/initiate',
	},
	myUrl: {
		1: '/pages/my/index',
		2: '/pages/my/index',
	},
}
/**
 * 校验手机号
 */
export const mobileCheck = (val) => {
	return mobileRegular.test(val)
}
/**
 * 校验身份证
 */
export const idCardCheck = (val) => {
	return idCardRegular.test(val)
}
/**
 *  拼接query
 * @param obj
 * @returns
 */
export const toQueryString = (obj) => {
	const keys = Object.keys(params)
	if (keys.length > 0) {
		return keys.map(key => `${key}=${params[key]}`).join('&')
	}
	return ''
}
/**
 * 判断一个对象是否为空
 * @param obj
 * @returns {number}
 */
export const isObjectEmpty = (obj) => {
	const filteredArr = Object.values(obj).filter((element) => element !== '');
	return filteredArr.length
}
//判断一个值在数组里是否存在
export const inArray = (val, arr) => {
	var arr = arr ? arr : [];
	return arr.some(item => {
		return item == val;
	})
}
/**
 * 判断一个值时候在一个数组对象里
 * @param arr
 * @param _index 对比数组对象索引
 * @param val
 * @returns {*}
 */
export const inObject = (arr, _index, val) => {
	return arr.some(item => {
		if (_index) {
			return item[_index] == val;
		}
		return item == val;
	})
}
/**
 * 对比数组长度
 * @param arr
 * @param arr2
 * @returns {boolean}
 */
export const eqLength = (arr, arr2) => {
	return arr.length == arr2.length;
}
/**
 * 提示框
 * @param msg
 */
export const msg = (msg) => {
	uni.showToast({
		title: msg,
		icon: 'none',
		duration: 1000,
	});
}
/**
 * 跳转页面
 * @param url
 */
export const goNav = (url) => {
	uni.navigateTo({
		url: url,
		animationType: 'pop-in',
		animationDuration: 200
	});
}
export const goRedirect = (url) => {
	uni.redirectTo({
		url: url,
		animationType: 'pop-in',
		animationDuration: 200
	});
}
/**
 * 返回上一页面
 * @param delta
 */
export const goBack = (delta) => {
	uni.navigateBack({
		delta: delta || 1,
	})
}
/**
 * tab导航切换
 * @param url
 */
export const goSwitchTab = (url) => {
	uni.switchTab({
		url: url,
		animationType: 'pop-in',
		animationDuration: 200,
		success: function(e) {
			var page = getCurrentPages().pop();
			if (page == undefined || page == null) return;
			page.onLoad();
		}
	});
}
/**
 * 获取数组对象里的对应索引，返回数组
 * @param array
 * @param _index 数组对象索引
 * @returns {*[]}
 */
export const arrayPluck = (array, _index) => {
	let res = [];
	array.forEach(item => {
		res.push(item[_index]);
	});
	return res;
}
/**
 * 格式化年月日
 * @param type
 * @param value
 * @returns {*|string}
 */
export const formatter = (type, value) => {
	if (type === 'year') {
		return `${value}年`
	}
	if (type === 'month') {
		return `${value}月`
	}
	if (type === 'day') {
		return `${value}日`
	}
	return value
}
/**
 * 根据给出索引，过滤数组
 * @param arr
 * @param changeIndex
 * @returns {*}
 */
export const arrayFilter = (arr, changeIndex) => {
	return arr.filter((item, index) => {
		return index != changeIndex;
	})
}
/**
 * 获取数组对象选中值
 * @param arr
 * @returns {*[]}
 */
export const changeItem = (arr) => {
	let changeItemVal = [];
	arr.forEach(item => {
		if (item.isCheck) {
			changeItemVal.push(item);
		}
	});
	return changeItemVal;
}
/**
 * 追加数组
 * @param arr
 * @param arr2
 * @returns {*}
 */
export const arrayPush = (arr, arr2) => {
	arr2.forEach((item) => {
		arr.push(item)
	})
	return arr;
}
export const objClear = (obj) => {
	if (isObjectEmpty(obj)) {
		return obj;
	}
	let keys = Object.keys(obj);
	let returnObj = {};
	for (let key of keys) {
		returnObj[key] = typeof(obj[key]) == 'array' ? [] : '';
	}
	return returnObj;
}
/**
 * 为数组中每个元素，最佳元素
 * @param arr
 * @param append
 * @returns {*}
 */
export const arrayAppend = (arr, append) => {
	return arr.map(item => {
		return {
			...item,
			...append
		}
	})
}
export const arrayGet = (obj, _index, defaultVal) => {
	defaultVal = defaultVal || '';
	let _indexArr = _index.split('.');
	let res = obj;
	_indexArr.forEach((item) => {
		res = typeof(res[item]) == 'undefined' ? '' : res[item];
		if (!res) {
			return defaultVal;
		}
	});
	return res;
}
export const nowDate = () => {
	const now = new Date();
	now.setMonth(now.getMonth() + 1)
	// 获取年份
	const year = now.getFullYear()
	// 获取月份
	let month = now.getMonth() + 1;
	month = month > 9 ? month : `0${month}`;
	// 获取日期
	let day = now.getDate()
	day = day > 9 ? day : `0${day}`;
	return `${year}-${month}-${day}`;
}
export const validateArray = (rule, value, callback) => {
	if (Array.isArray(value) && value.length > 0) {
		callback();
	} else {
		callback(new Error('必填项不能为空'));
	}
}
// 时间戳格式化 
export const timestampToDateFormat = (timestamp) => {
	const dateObj = new Date(timestamp);
	const year = String(dateObj.getFullYear());
	const month = String(dateObj.getMonth() + 1).padStart(2, "0");
	const day = String(dateObj.getDate()).padStart(2, "0");
	return year + "-" + month + "-" + day;
}
export const upload = (options, callback) => {
	const tempPath = options.tempPath;
	const tempName = options.tempName;
	const tempExt = options.tempExt;
	const tempTurn = options.turn || 0;
	const images = [];
	uni.getFileSystemManager().readFile({
		filePath: tempPath, // 要读取的文件路径
		encoding: 'base64', // 编码格式
		success: function(res) {
			let params = {
				ext: tempExt,
				content: res.data,
				org_name: tempName,
				turn: tempTurn,
			}
			return uploadImageBase64(params).then(res => {
				images.push(res.id)
				if (typeof(callback) == 'function') {
					callback({
						id: images,
						res: res,
					});
				}
			});
		}
	});
	return images;
}
export const phoneSecret = (phone) => {
	return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
}
export const checkPermissions = (rule) => {
	const permissions = uni.getStorageSync("permissions")
	return inArray(rule, permissions);
}